package com.mjlf.t.t1.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

/**
 * @ClassName TestClientInitializer
 * @auther mjlf
 * @Date 2019/9/17 10:34
 * @Description TODO
 */
public class TestClientInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4))
                //计算当前待发送消息的二进制字节长度，将该长度添加到ByteBuf的缓冲区头中
                .addLast(new LengthFieldPrepender(4))
                //将byte数据解码成String
                .addLast(new StringEncoder(CharsetUtil.UTF_8))
                //将字符串编码成byte数据
                .addLast(new StringDecoder(CharsetUtil.UTF_8))
                .addLast(new TestClientHandler());
    }
}
